package com.dobby.demo.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

/**
 * Created by liguoqing on 2016/2/29.
 */
@Repository
public class OrderDao {

    @Autowired
    private RedisTemplate<String, Order> redisTemplate;

    /**
     * 保存order到redis库中
     * @param order
     */
    public void save(Order order) {
        ValueOperations<String, Order> valueOper = redisTemplate.opsForValue();
        valueOper.set(order.getId(), order);
    }

    /**
     * 根据id删除redis中的数据
     * @param id
     */
    public void delete(String id) {
        ValueOperations<String, Order> valueOper = redisTemplate.opsForValue();
        RedisOperations<String, Order> redisOper = valueOper.getOperations();
        redisOper.delete(id);
    }

    /**
     * 根据id读取redis库中的数据
     * @param id
     * @return
     */
    public Order read(String id) {
        ValueOperations<String,Order> valueOper = redisTemplate.opsForValue();
        return valueOper.get(id);
    }


}
